import Mock from 'mockjs'
const List = []
const count = 20
for (let i = 0; i < count; i++) {
  List.push(Mock.mock({
    id: '@increment',
    name: '@cname',
    'role|1': ['超级管理员', '管理员', '普通用户'],
    datetime: '@datetime',
    address: '@city(true)',
    description: '@csentence(10, 20)'
  }))
}
export default [
  // 模拟所有的用户分页展示
  {
    url: '/user/list',
    type: 'get',
    response: config => {
      const { role, name, page = 1, limit = 20, sort } = config.query
      let mockList = List.filter(item => {
        if (role && item.role !== role) return false
        if (name && item.name.indexOf(name) < 0) return false
        return true
      })

      if (sort === '-id') {
        mockList = mockList.reverse()
      }

      const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

      return {
        code: 20000,
        data: {
          total: mockList.length,
          items: pageList
        }
      }
    }
  },

  // 添加用户
  {
    url: '/user/create',
    type: 'post',
    response: {
      code: 20000,
      message: '添加成功',
      data: {
        status: 'success'
      }
    }
  },

  // 修改用户
  {
    url: '/user/update',
    type: 'put',
    response: {
      code: 20000,
      message: '修改成功',
      data: {
        status: 'success'
      }
    }
  },

  // 删除用户
  {
    url: '/user/[A-Za-z0-9]',
    type: 'delete',
    response: {
      code: 20000,
      message: '删除成功',
      data: {
        status: 'success'
      }
    }
  }
]
